package com.caigou.modules.login.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.UserBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.login.repository.LoginRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/27
 */
@HiltViewModel
class LoginViewModel @Inject constructor(private val repository: LoginRepository) : ViewModel() {

    val userLiveData = MutableLiveData<Resource<UserBean>>()

    val smsCodeLiveData = MutableLiveData<Resource<String>>()

    val forgetLiveData = MutableLiveData<Resource<String>>()

    fun launcher(mobile: String, password: String) {
        userLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.launcher(mobile, password)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserBean>(-1, message)
                }
                .collectLatest {
                    userLiveData.value = it
                }
        }
    }

    fun getSmsCode(mobile: String) {
        smsCodeLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getSmsCode(mobile)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                }
                .collectLatest {
                    smsCodeLiveData.value = it
                }
        }
    }

    fun register(nickName: String, mobile: String, password: String, smsCode: String, invitationCode: String) {
        userLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.register(nickName, mobile, password, smsCode, invitationCode)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserBean>(-1, message)
                    Logger.e("catch ${it.stackTrace}")
                }
                .collectLatest {
                    userLiveData.value = it
                }
        }
    }

    fun forgetPwd(mobile: String, smsCode: Long, newPwd: String) {
        forgetLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.forgetPwd(mobile, smsCode, newPwd)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                }.collectLatest {
                    forgetLiveData.value = it
                }
        }
    }

}